<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { View, Delete } from '@element-plus/icons-vue'

interface Order {
  id: string
  packageName: string
  amount: number
  status: 'pending' | 'completed' | 'cancelled'
  createTime: string
}

const activeTab = ref<string>('all')

const orders = ref<Order[]>([
  {
    id: 'ORD20250101001',
    packageName: '基础套餐 - 单次发布',
    amount: 99,
    status: 'completed',
    createTime: '2025-01-15 10:30:00'
  },
  {
    id: 'ORD20250105002',
    packageName: '标准套餐 - 月度会员',
    amount: 299,
    status: 'pending',
    createTime: '2025-01-20 14:20:00'
  },
  {
    id: 'ORD20250110003',
    packageName: '高级套餐 - 年度会员',
    amount: 1999,
    status: 'completed',
    createTime: '2025-02-01 09:15:00'
  },
  {
    id: 'ORD20250112004',
    packageName: '基础套餐 - 单次发布',
    amount: 99,
    status: 'cancelled',
    createTime: '2025-02-10 16:45:00'
  }
])

const filteredOrders = ref<Order[]>([])

const updateFilteredOrders = () => {
  if (activeTab.value === 'all') {
    filteredOrders.value = orders.value
  } else {
    filteredOrders.value = orders.value.filter(order => order.status === activeTab.value)
  }
}

updateFilteredOrders()

const handleTabChange = (tab: string) => {
  activeTab.value = tab
  updateFilteredOrders()
}

const getStatusText = (status: string): string => {
  const statusMap: Record<string, string> = {
    pending: '待支付',
    completed: '已完成',
    cancelled: '已取消'
  }
  return statusMap[status] || status
}

const getStatusType = (status: string): 'warning' | 'success' | 'info' => {
  const typeMap: Record<string, 'warning' | 'success' | 'info'> = {
    pending: 'warning',
    completed: 'success',
    cancelled: 'info'
  }
  return typeMap[status] || 'info'
}

const handleViewDetail = (order: Order) => {
  ElMessage.info(`查看订单详情：${order.id}`)
}

const handleCancelOrder = (order: Order) => {
  ElMessageBox.confirm('确定要取消该订单吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    order.status = 'cancelled'
    ElMessage.success('订单已取消')
  }).catch(() => {
    // 用户取消操作
  })
}
</script>

<template>
  <div class="order-list">
    <div class="list-header">
      <h3 class="list-title">我的订单</h3>
      <p class="list-desc">查看和管理您的订单记录</p>
    </div>

    <!-- Tab切换 -->
    <div class="order-tabs">
      <div
        class="tab-item"
        :class="{ active: activeTab === 'all' }"
        @click="handleTabChange('all')"
      >
        全部订单
      </div>
      <div
        class="tab-item"
        :class="{ active: activeTab === 'pending' }"
        @click="handleTabChange('pending')"
      >
        待支付
      </div>
      <div
        class="tab-item"
        :class="{ active: activeTab === 'completed' }"
        @click="handleTabChange('completed')"
      >
        已完成
      </div>
      <div
        class="tab-item"
        :class="{ active: activeTab === 'cancelled' }"
        @click="handleTabChange('cancelled')"
      >
        已取消
      </div>
    </div>

    <!-- 订单列表 -->
    <div class="order-cards">
      <div v-if="filteredOrders.length === 0" class="empty-state">
        <div class="empty-icon">📦</div>
        <div class="empty-text">暂无订单记录</div>
      </div>

      <div
        v-for="order in filteredOrders"
        :key="order.id"
        class="order-card"
      >
        <div class="card-header">
          <div class="order-id">订单号：{{ order.id }}</div>
          <el-tag :type="getStatusType(order.status)" size="large">
            {{ getStatusText(order.status) }}
          </el-tag>
        </div>

        <div class="card-body">
          <div class="order-info">
            <div class="package-name">{{ order.packageName }}</div>
            <div class="order-time">下单时间：{{ order.createTime }}</div>
          </div>
          <div class="order-amount">
            <span class="amount-label">订单金额</span>
            <span class="amount-value">¥{{ order.amount }}</span>
          </div>
        </div>

        <div class="card-footer">
          <el-button
            :icon="View"
            size="default"
            @click="handleViewDetail(order)"
          >
            查看详情
          </el-button>
          <el-button
            v-if="order.status === 'pending'"
            :icon="Delete"
            type="danger"
            plain
            size="default"
            @click="handleCancelOrder(order)"
          >
            取消订单
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.order-list {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  padding: 32px;
  box-shadow: 0 4px 16px rgba(79, 172, 254, 0.1);
}

.list-header {
  margin-bottom: 24px;
  padding-bottom: 20px;
  border-bottom: 2px solid #f0f9ff;
}

.list-title {
  font-size: 22px;
  font-weight: 700;
  color: var(--text-primary);
  margin: 0 0 8px 0;
}

.list-desc {
  font-size: 14px;
  color: var(--text-secondary);
  margin: 0;
}

/* Tab切换 */
.order-tabs {
  display: flex;
  gap: 12px;
  margin-bottom: 24px;
  flex-wrap: wrap;
}

.tab-item {
  padding: 10px 24px;
  border-radius: 24px;
  font-size: 15px;
  font-weight: 500;
  color: var(--text-secondary);
  background: #f5f5f5;
  cursor: pointer;
  transition: all 0.3s ease;
}

.tab-item:hover {
  background: linear-gradient(135deg, rgba(79, 172, 254, 0.1) 0%, rgba(0, 242, 254, 0.1) 100%);
  color: var(--primary-color);
}

.tab-item.active {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: #ffffff;
  box-shadow: 0 4px 12px rgba(79, 172, 254, 0.25);
}

/* 订单卡片 */
.order-cards {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 16px;
}

.empty-text {
  font-size: 16px;
  color: var(--text-secondary);
}

.order-card {
  border: 2px solid #f0f0f0;
  border-radius: 12px;
  padding: 20px;
  transition: all 0.3s ease;
}

.order-card:hover {
  border-color: var(--primary-color);
  box-shadow: 0 4px 16px rgba(79, 172, 254, 0.15);
  transform: translateY(-2px);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f0f0f0;
}

.order-id {
  font-size: 14px;
  color: var(--text-secondary);
  font-family: monospace;
}

.card-body {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.order-info {
  flex: 1;
}

.package-name {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 8px;
}

.order-time {
  font-size: 13px;
  color: var(--text-secondary);
}

.order-amount {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}

.amount-label {
  font-size: 13px;
  color: var(--text-secondary);
}

.amount-value {
  font-size: 24px;
  font-weight: 700;
  color: #ff6b6b;
}

.card-footer {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .order-list {
    padding: 20px;
  }

  .card-body {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .order-amount {
    align-items: flex-start;
  }

  .card-footer {
    width: 100%;
    flex-direction: column;
  }

  .card-footer .el-button {
    width: 100%;
  }
}
</style>
